<template>
  <div>
    <h2>理财添加</h2>
    <table class="table table-bordered">
      <tr>
        <td>理财名称</td>
        <td>
          <input type="text" v-model="state.F_name" class="form-control">
        </td>
      </tr>
      <tr>
        <td>年收益</td>
        <td>
          <input type="text" v-model="state.F_yearPrice" class="form-control">
        </td>
      </tr>
      <tr>
        <td>利息</td>
        <td>
          <select v-model="state.I_id" class="form-control">
            <option value="0">--请选择--</option>
            <option v-for="item in list" :value="item.I_id">{{ item.I_name }}</option>
          </select>
        </td>
      </tr>
      <tr>
        <td>最低金额</td>
        <td>
          <input type="text" v-model="state.F_mixPrice" class="form-control">
        </td>
      </tr>
      <tr>
        <td>风险等级</td>
        <td>
          <select v-model="state.F_Grade" class="form-control">
            <option value="0">--请选择--</option>
            <option value="1">低</option>
            <option value="2">中</option>
            <option value="3">高</option>
          </select>
        </td>
      </tr>
      <tr>
        <td colspan="2">
          <input type="button" @click="add" value="添加" class="btn btn-primary" >
        </td>
      </tr>
    </table>
  </div>
</template>
<script setup lang="ts">  
import {reactive,toRefs,ref,onMounted} from 'vue'
import { useRouter } from 'vue-router'
const router=useRouter()
import axios from 'axios'

const data = reactive({
  state:{
    F_name:'',
    F_yearPrice:'',
    I_id:'0',
    F_mixPrice:'',
    F_num:0,
    F_Grade:0,
  }
})

const {state}=toRefs(data)


const list=ref([{
  I_id:'',
  I_name:''
}])

onMounted(()=>{
  BackFill()
})

const BackFill=()=>{
  axios({
    url:'http://localhost:54566/api/Finance/GetInverestData',
    mathod:'get',
  })
  .then((res)=>{
    list.value=res.data
  })
  .catch((err)=>{
    console.log(err)
  })
}


const add=()=>{
  if(state.value.F_name==''){
    alert('理财名称不能为空')
    return
  }
  if(state.value.F_mixPrice==0){
    alert('最低金额必须大于0')
    return
  }
  axios({
    url:'http://localhost:54566/api/Finance/PostFinanceInsert',
    method:'post',
    data:state.value
  })
  .then((res)=>{
    console.log(res.data)
    if(res.data==-1){
      alert('理财项目名称不能重复')
      return  
    }
    if(res.data>0){
      alert('添加成功')
      router.push('/FinanceList')
    }else{

      alert('添加失败')
      return
    }
  })
}

</script>